<?php
namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Request;
use think\Validate;
use think\Session;
use app\admin\model\Nav;
use app\admin\model\Label;
use app\admin\model\Article;
use app\admin\model\User;
use app\admin\model\Comments;

class Index extends Controller
{
    public function __construct(){
        $user = Session::has('user');
        if ($user) {
            $user = Session::get('user');
        }else{
            $this->success('未登录', 'login/login');
        }
        parent::__construct();
    }

    public function index()
    {
        return $this->fetch();
    }

    public function article_list()
    {
        $article = new Article();
        $nav = new Nav();
        $nav_list = $nav->get_NavList();
        $data = $article->get_Article();
        if (Request::instance()->isPost()) {
            $data = input('post.');
            if ($data['cat_id']==true) {
                $where['nav_id'] = $data['cat_id'];
            }
            if ($data['time1'] && $data['time2']) {
                $time1 = strtotime($data['time1']);
                $time2 = strtotime($data['time2']);
                $where['addtime'] = array('between', array($time1,$time2));
            }
            $data = $article->get_Article($where);
        }
        $this->assign([
            'data'  =>  $data,
            'nav'   =>  $nav_list,
            'time'  =>  time(),
        ]);
        return $this->fetch();
    }

    public function article_add()
    {
        $nav = new Nav();
        $user = new User();
        $article = new Article();
        $nav_cat = $nav->getNavCatListTop();
        $nav_list = $nav->get_NavList();
        $user_list = $user->get_Member();
        if (Request::instance()->isPost()) {
            $file = request()->file('file');
            if($file){
                $info = $file->move(ROOT_PATH . 'public' . DS . 'images');
                if($info){
                    $data['img'] = '/public' . '/images/' . date("Ymd") . '/' . $info->getFilename();
                }else{
                    // 上传失败获取错误信息
                    echo $file->getError();
                    $this->getError("上传出错");die;
                }
            }
            $data = input('post.');
            $data['addtime'] = time();
            unset($data['file']);
            unset($data['cat_id']);
            $nav_one = $nav->get_NavOne($data['nav_id']);
            $data['nav_name'] = $nav_one['name'];
            $user_one = $user->get_MemberOne($data['user_id']);
            $data['member_name'] = $user_one['member_name'];
            $res = $article->get_ArticleAdd($data);
            if ($res) {
                $res1 = $user->getAddLike($data['user_id']);
                $this->success('新增成功');
            }else{
                $this->error('新增失败');
            }
        }
        $this->assign([
            'nav'   =>  $nav_list,
            'user'  =>  $user_list,
            'nav_cat'   =>  $nav_cat
        ]);
        return $this->fetch();
    }

    public function article_nav()
    {
        $nav = new Nav();
        $cat_id = input('param.cat_id');
        $where['cat_id'] = $cat_id;
        $nav_list = $nav->get_NavList($where);
        return json($nav_list);
    }

    public function label()
    {
        $label = new Label();
        $data = $label->get_LabelList();
        $this->assign([
            'data'  =>  $data
        ]);
        return $this->fetch();
    }

    public function article_edit()
    {
        $nav = new Nav();
        $user = new User();
        $m_label = new Label();
        $article = new Article();
        $nav_list = $nav->get_NavList();
        $user_list = $user->get_Member();
        $id = input('param.id');
        $data = $article->get_ArticleOne($id);
        $label = explode(",",$data['label']);
        $label_v = '';
        foreach ($label as $key => $value) {
            if ($value !='') {
                $label_v[$key] = $m_label->get_LabelOne($value);
            }
        }
        $data['label_v'] = $label_v;
        if (Request::instance()->isPost()) {
            $file = request()->file('file');
            $data = input('post.');
            if($file){
                $info = $file->move(ROOT_PATH . 'public' . DS . 'images');
                if($info){
                    $data['img'] = '/public' . '/images/' . date("Ymd") . '/' . $info->getFilename();
                }else{
                    // 上传失败获取错误信息
                    echo $file->getError();
                    $this->getError("上传出错");die;
                }
            }
            $data['addtime'] = time();
            unset($data['file']);
            $nav_one = $nav->get_NavOne($data['nav_id']); 
            $data['nav_name'] = $nav_one['name'];
            $user_one = $user->get_MemberOne($data['user_id']);
            $data['member_name'] = $user_one['member_name'];
            $res = $article->get_ArticleEdit($data,$id);
            if ($res) {
                $this->success('修改成功','index/article_list');
            }else{
                $this->error('修改失败');
            }
        }
        $this->assign([
            'nav'   =>  $nav_list,
            'user'  =>  $user_list,
            'data'  =>  $data
        ]);
        return $this->fetch();
    }

    public function article_del()
    {
        $article = new Article();
        $id = input('param.id');
        $res = $article->Del_Article($id);
        if ($res) {
            return 200;
        }else{
            return 400;
        }
    }
    
    /*文章评论*/
    public function comments_list()
    {
        $nav = new Nav();
        $comments = new Comments();
        $nav_list = $nav->get_NavList();
        $data = $comments->get_CommentsList();
        $this->assign([
            'data'  =>  $data,
            'nav'   =>  $nav_list,
            'time'  =>  time(),
        ]);
        return $this->fetch();
    }

    public function nav_cat_list()
    {
        $nav = new Nav();
        $nav_list = $nav->get_NavCatList();
        $this->assign([
            'data'  =>  $nav_list
        ]);
        return $this->fetch();
    }

    public function nav_cat_add()
    {
        $nav = new Nav();
        if (Request::instance()->isPost()) {
            $data = input('post.');
            $data['addtime'] = time();
            $data['top_id'] = 0;
            $data['level'] = 0;
            $res = $nav->get_NavCatAdd($data);
            if ($res) {
                $this->success('新增成功');
            }else{
                $this->error('新增失败');
            }
        }
        return $this->fetch();
    }

    public function nav_cat_edit()
    {
        $nav = new Nav();
        $id = input('param.id');
        $nav_list = $nav->get_NavCatOne($id);
        if (Request::instance()->isPost()) {
            $data = input('post.');
            $data['addtime'] = time();
            $res = $nav->get_NavCatEdit($data,$id);
            if ($res) {
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }
        }
        $this->assign([
            'data'  =>  $nav_list
        ]);
        return $this->fetch();
    }

    public function nav_cat_del()
    {
        $nav = new Nav();
        $id = input('param.id');
        $res = $nav->get_NavCatDel($id);
        if ($res) {
            return 200;
        }else{
            return 400;
        }
    }

    public function nav_list()
    {
        $nav = new Nav();
        $nav_list = $nav->get_NavList();
        foreach ($nav_list as $key => $value) {
            $nav_cat = $nav->get_NavCatOne($value['cat_id']);
            $nav_list[$key]['cat_name'] = $nav_cat['name'];
        }
        $this->assign([
            'data'  =>  $nav_list
        ]);
        return $this->fetch();
    }

    public function nav_add()
    {
        $nav = new Nav();
        $nav_cat_list = $nav->get_NavCatList();
        if (Request::instance()->isPost()) {
            $data = input('post.');
            $data['addtime'] = time();
            $res = $nav->get_NavAdd($data);
            if ($res) {
                $this->success('新增成功');
            }else{
                $this->error('新增失败');
            }
        }
        $this->assign([
            'cat_list'  =>  $nav_cat_list
        ]);
        return $this->fetch();
    }

    public function nav_edit()
    {
        $nav = new Nav();
        $id = input('param.id');
        $nav_cat_list = $nav->get_NavCatList();
        $data = $nav->get_NavOne($id);
        if (Request::instance()->isPost()) {
            $data = input('post.');
            $data['addtime'] = time();
            $res = $nav->get_NavEdit($data,$id);
            if ($res) {
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }
        }
        $this->assign([
            'cat_list'  =>  $nav_cat_list,
            'data'      =>  $data
        ]);
        return $this->fetch();
    }

    public function nav_del()
    {
        $nav = new Nav();
        $id = input('param.id');
        $res = $nav->get_NavDel($id);
        if ($res) {
            return 200;
        }else{
            return 400;
        }
    }

    public function label_list()
    {
        $Label = new Label();
        $label_list = $Label->get_LabelList();
        $this->assign([
            'data'   =>    $label_list
        ]);
        return $this->fetch();
    }

    public function label_add()
    {
        $nav = new Nav();
        $label = new Label();
        $nav_list = $nav->get_NavList();
        if (Request::instance()->isPost()) {
            $data = input('post.');
            $nav = $nav->get_NavOne($data['cat_id']);
            $data['cat_name'] = $nav['name'];
            $data['addtime'] = time();
            $res = $label->get_LabelAdd($data);
            if ($res) {
                $this->success('新增成功');
            }else{
                $this->error('新增失败');
            }
        }
        $this->assign([
            'nav_list'   =>    $nav_list
        ]);
        return $this->fetch();
    }

    public function label_edit()
    {
        $nav = new Nav();
        $label = new Label();
        $id = input('param.id');
        $nav_list = $nav->get_NavList();
        $data = $label->get_LabelOne($id);
        if (Request::instance()->isPost()) {
            $data = input('post.');
            $nav = $nav->get_NavOne($data['cat_id']);
            $data['cat_name'] = $nav['name'];
            $data['addtime'] = time();
            $res = $label->get_LabelEdit($data,$id);
            if ($res) {
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }
        }
        $this->assign([
            'data'  =>  $data,
            'nav_list'  =>  $nav_list
        ]);
        return $this->fetch();
    }

    public function label_Del()
    {
        $label = new Label();
        $id = input('param.id');
        $res = $label->get_LabelDel($id);
        if ($res) {
            return 200;
        }else{
            return 400;
        }
    }

    public function welcome()
    {
        return $this->fetch();
    }
}
